<template>
  <div class="category-banner-wrapper">
    <!-- 左侧分类 -->
    <div class="category-left">
      <ul>
        <li
          v-for="item in categories"
          :key="item.id"
          :class="{ active: activeCategory && activeCategory.id === item.id }"
          @mouseenter="showSubMenu(item)"
          @mouseleave="hideSubMenu"
        >
          <span>{{ item.name }}</span>
        </li>
      </ul>
    </div>
    <!-- 右侧Banner -->
    <div class="banner-right">
      <HomeBanner />
      <!-- 悬浮二级/三级分类菜单 -->
      <div
        class="category-float-menu"
        v-if="showMenu && activeCategory"
        @mouseenter="menuHover = true"
        @mouseleave="menuHover = false; hideSubMenu()"
      >
        <div class="sub-category-group" v-for="sub in activeCategory.children" :key="sub.id">
          <div class="sub-category-title" @click="goToSearch(sub)" style="cursor:pointer">{{ sub.name }}</div>
          <div class="sub-category-list" v-if="sub.children && sub.children.length">
            <span v-for="child in sub.children" :key="child.id" class="sub-category-item" @click="goToSearch(child)" style="cursor:pointer">
              {{ child.name }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import HomeBanner from './HomeBanner.vue'
import { getCategoryInfo } from '@/api/category'
import { useRouter } from 'vue-router'
import { useSearchStore } from '@/stores/search'

const categories = ref([])
const activeCategory = ref(null)
const showMenu = ref(false)
const menuHover = ref(false)
const floatMenuStyle = ref({})
let hideTimer = null
const router = useRouter()
const searchStore = useSearchStore()

onMounted(async () => {
  const res = await getCategoryInfo()
  categories.value = res.data || res || []
})

// 鼠标移入主分类，显示浮层
function showSubMenu(item) {
  if (hideTimer) {
    clearTimeout(hideTimer)
    hideTimer = null
  }
  activeCategory.value = item
  showMenu.value = true
  menuHover.value = false
}

// 鼠标离开主分类或浮层，延迟隐藏（防止抖动）
function hideSubMenu() {
  hideTimer = setTimeout(() => {
    if (!menuHover.value) {
      showMenu.value = false
      activeCategory.value = null
    }
  }, 100)
}

function goToSearch(category) {
  searchStore.setKeyword(category.name)
  router.push({ path: '/search', query: { keyword: category.name } })
}
</script>

<style scoped>
.category-banner-wrapper {
  display: flex;
  width: 1200px;
  margin: 0 auto 30px auto;
  min-height: 400px;
  position: relative;
  align-items: stretch;
}
.category-left {
  width: 200px;
  background: #fff;
  border-radius: 16px 0 0 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  padding: 10px 0;
  z-index: 10;
  min-height: 400px;
  height: 400px;
  display: flex;
  flex-direction: column;
}
.category-left ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.category-left li {
  padding: 14px 24px;
  cursor: pointer;
  font-size: 16px;
  color: #333;
  transition: background 0.2s;
  position: relative;
}
.category-left li.active,
.category-left li:hover {
  background: #f5f5f5;
  color: #e1251b;
}
.banner-right {
  flex: 1;
  min-width: 0;
  background: #fff;
  border-radius: 0 16px 16px 0;
  overflow: hidden;
  position: relative;
  z-index: 1;
  margin-left: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  height: 400px;
}
.banner-right > * {
  width: 100%;
  height: 100%;
  border-radius: 0 16px 16px 0;
  object-fit: cover;
  background: #f5f5f5;
  display: block;
}
.category-float-menu {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-width: unset;
  max-width: unset;
  background: #fff;
  box-shadow: 0 4px 24px rgba(0,0,0,0.12);
  border-radius: 0 8px 8px 0;
  padding: 32px 40px 24px 40px;
  display: flex;
  flex-wrap: wrap;
  z-index: 10;
  animation: fadeIn 0.2s;
  align-items: flex-start;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px);}
  to { opacity: 1; transform: translateY(0);}
}
.sub-category-group {
  margin-right: 40px;
  margin-bottom: 20px;
  min-width: 120px;
}
.sub-category-title {
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
}
.sub-category-list {
  margin-top: 6px;
}
.sub-category-item {
  display: inline-block;
  margin-right: 12px;
  margin-bottom: 6px;
  padding: 4px 10px;
  background: #f5f5f5;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.2s;
}
.sub-category-item:hover {
  background: #e1251b;
  color: #fff;
}
</style>
